Option Explicit
Const cSolutionId = "7D395EDB-7A50-4179-8E06-37235B1006CB"
Dim oFS, oManifest, args, iTemplateFileCount, iFeatureCount, iSiteDefinitionCount, iRootFileCount

Dim arTemplateFiles(10000)
Dim arFeatures(1000)
Dim arSiteDefinitionManifests(1000)
Dim arRootFiles(10000)

iTemplateFileCount = 0
iFeatureCount = 0
iSiteDefinitionCount = 0
iRootFileCount = 0

Set args = WScript.Arguments

' Create the File System Object
Set oFS = CreateObject("Scripting.FileSystemObject")

Set oManifest = oFS.CreateTextFile(args(0) + "manifest.xml", true)
oManifest.WriteLine "<Solution xmlns='http://schemas.microsoft.com/sharepoint/' SolutionId='" + cSolutionId + "'>"

Dim oFile, oGacFolder, oBinFolder, oCasFolder
Set oGacFolder = oFS.GetFolder(args(0) + "DLLS\GAC")
Set oBinFolder = oFS.GetFolder(args(0) + "DLLS")
Set oCasFolder = oFS.GetFolder(args(0) + "DLLS\CodeAccessSecurity")

'Add <Assemblies/> section
If (oGacFolder.Files.Count > 0 Or oBinFolder.Files.Count > 0) Then
    Dim sSafeControls, oSafeXml
    
    oManifest.WriteLine WriteTab(1) + "<Assemblies>"

    For each oFile in oGacFolder.Files
        oManifest.WriteLine WriteTab(2) + "<Assembly DeploymentTarget='GlobalAssemblyCache' Location='GAC\" + oFile.Name + "'>"
        
        If (oFS.FileExists(args(0) + "DLLS\SafeControls\" + oFile.Name + ".xml")) Then
            Set oSafeXml = oFS.OpenTextFile(args(0) + "DLLS\SafeControls\" + oFile.Name + ".xml")
            
            sSafeControls = oSafeXml.ReadAll
            oManifest.Write Mid(sSafeControls, 4)            
            oManifest.WriteBlankLines 1
            oSafeXml.Close
        End If
        oManifest.WriteLine WriteTab(2) + "</Assembly>"
    Next

    For each oFile in oBinFolder.Files
        oManifest.WriteLine WriteTab(2) + "<Assembly DeploymentTarget='WebApplication' Location='" + oFile.Name + "'>"
        
        If (oFS.FileExists(args(0) + "DLLS\SafeControls\" + oFile.Name + ".xml")) Then
            Set oSafeXml = oFS.OpenTextFile(args(0) + "DLLS\SafeControls\" + oFile.Name + ".xml")
            
            sSafeControls = oSafeXml.ReadAll
            oManifest.Write Mid(sSafeControls, 4)            
            oManifest.WriteBlankLines 1
            oSafeXml.Close
        End If
        oManifest.WriteLine WriteTab(2) + "</Assembly>"
    next

    oManifest.WriteLine WriteTab(1) + "</Assemblies>"

End If

'Add <CodeAccessSecurity/> section
If (oCasFolder.Files.Count > 0) Then
    Dim sCodeAccessSecurity, oCASXml    

    oManifest.WriteLine WriteTab(1) + "<CodeAccessSecurity>"

    For each oFile in oCasFolder.Files
        Set oCASXml = oFS.OpenTextFile(args(0) + "DLLS\CodeAccessSecurity\" + oFile.Name)
                
        sCodeAccessSecurity = oCASXml.ReadAll
        oManifest.Write Mid(sCodeAccessSecurity, 4)            
        oManifest.WriteBlankLines 1
        oCASXml.Close
    Next

    oManifest.WriteLine WriteTab(1) + "</CodeAccessSecurity>"
    

End If

'Parse 12 hive
EnumFolder args(0) + "12", ""

'Add <FeatureManifests/> section
If iFeatureCount > 0 Then
    oManifest.WriteLine WriteTab(1) + "<FeatureManifests>"

    Dim sFeatureManifest
    For Each sFeatureManifest In arFeatures
        If sFeatureManifest <> "" Then oManifest.WriteLine WriteTab(2) + "<FeatureManifest Location='" + sFeatureManifest + "' />"
    Next

    oManifest.WriteLine WriteTab(1) + "</FeatureManifests>"
End If

'Add <RootFiles/> section
If iRootFileCount > 0 Then
    oManifest.WriteLine WriteTab(1) + "<RootFiles>"

    Dim sRootFile
    For Each sRootFile In arRootFiles
        If sRootFile <> "" Then oManifest.WriteLine WriteTab(2) + "<RootFile Location='" + sRootFile + "' />"
    Next

    oManifest.WriteLine WriteTab(1) + "</RootFiles>"
End If

'Add <SiteDefinitionManifests/> section
If iSiteDefinitionCount > 0 Then
    Dim sWebTempFile, oWebTempXml

    oManifest.WriteLine WriteTab(1) + "<SiteDefinitionManifests>"

    Dim sSiteDefinitionManifest
    For Each sSiteDefinitionManifest In arSiteDefinitionManifests
        If sSiteDefinitionManifest <> "" Then
            oManifest.WriteLine WriteTab(2) + "<SiteDefinitionManifest Location='" + sSiteDefinitionManifest + "'>"
        
            If (oFS.FileExists(args(0) + "12\TEMPLATE\SiteTemplates\WEBTEMP\" + sSiteDefinitionManifest + ".xml")) Then
                Set oWebTempXml = oFS.OpenTextFile(args(0) + "12\TEMPLATE\SiteTemplates\WEBTEMP\" + sSiteDefinitionManifest + ".xml")
            
                sWebTempFile = oWebTempXml.ReadAll
                oManifest.Write Mid(sWebTempFile, 4)            
                oManifest.WriteBlankLines 1
                oWebTempXml.Close
            End If
                
            oManifest.WriteLine WriteTab(2) + "</SiteDefinitionManifest>"
        End If            
    Next

    oManifest.WriteLine WriteTab(1) + "</SiteDefinitionManifests>"
End If

'Add <TemplateFiles/> section
If iTemplateFileCount > 0 Then
    oManifest.WriteLine WriteTab(1) + "<TemplateFiles>"
    
    Dim sTemplateFile
    For Each sTemplateFile In arTemplateFiles
        If sTemplateFile <> "" Then oManifest.WriteLine WriteTab(2) + "<TemplateFile Location='" + sTemplateFile + "' />"
    Next

    oManifest.WriteLine WriteTab(1) + "</TemplateFiles>"
End If

oManifest.WriteLine "</Solution>"
oManifest.Close

Sub EnumFolder(sFolder, sRelativePath)
    
    Dim oFolder, oFolders, oSub, oFile
    Set oFolder = oFS.GetFolder(sFolder)

    'Skip 12\TEMPLATE\SiteTemplates\WEBTEMP folder
    If (oFolder.Name = "WEBTEMP" And oFolder.ParentFolder.Name = "SiteTemplates") Then Exit Sub
        
    If (oFolder.Name = "TEMPLATE" And oFolder.ParentFolder.Name = "12") Then sRelativePath = ""
    If (oFolder.Name = "FEATURES" And oFolder.ParentFolder.Name = "TEMPLATE") Then sRelativePath = ""
    If (oFolder.Name = "SiteTemplates" And oFolder.ParentFolder.Name = "TEMPLATE") Then sRelativePath = ""

    If (sRelativePath <> "") Then sRelativePath = sRelativePath + "\"
    
    Dim isFeature, isSiteTemplate
    isFeature = False
    isSiteTemplate = False
        
    If (sRelativePath <> "" And oFolder.ParentFolder.Name = "FEATURES") Then   
        iFeatureCount = iFeatureCount + 1
        arFeatures(iFeatureCount) = sRelativePath + "Feature.xml"
        isFeature = True
    End If

    If (sRelativePath <> "" And oFolder.ParentFolder.Name = "SiteTemplates") Then
        iSiteDefinitionCount = iSiteDefinitionCount + 1
        arSiteDefinitionManifests(iSiteDefinitionCount) = oFolder.Name
        isSiteTemplate = True
    End If

    If Not isFeature And Not isSiteTemplate Then
        For Each oFile In oFolder.Files

            If (sRelativePath <> "" And InStr(1, sFolder, "12\TEMPLATE") > 0) Then
                iTemplateFileCount = iTemplateFileCount + 1
                arTemplateFiles(iTemplateFileCount) = sRelativePath + oFile.Name
            Else
                iRootFileCount = iRootFileCount + 1            
                arRootFiles(iRootFileCount) = sRelativePath + oFile.Name                            
            End If
        
        Next
    End If

    If (sRelativePath <> "" And InStr(1, sFolder, "12\TEMPLATE\FEATURES") > 0) Then
       If (Left(sRelativePath,9) <> "FEATURES\") Then
          sRelativePath = "FEATURES\" + sRelativePath
       End If
    End If

    If Not isSiteTemplate Then
        For Each oSub in oFolder.SubFolders
            EnumFolder oSub.Path, sRelativePath + oSub.Name
        Next
    End If
    
End Sub

Function WriteTab(iCount)
    Dim RetValue, i
    For i = 1 To iCount
        RetValue = RetValue + vbTab        
    Next
    WriteTab = RetValue
End Function